const logger = require('../helper/logger')

const tableName = 'user'

module.exports = async (db) => {
  try {
    const exists = await db.schema.hasTable(tableName)
    if (!exists) {
      await db.schema.createTable(tableName, (table) => {
        table.charset('utf8')
        table.increments('id')
        table.string('full_name').notNullable()
        table.string('username')
        table.string('email').unique()
        table.string('password')
        table.string('phone_region')
        table.string('phone_number')
        table.unique(['phone_region', 'phone_number'])
        table.string('profile_image_url')
        table.text('headline')
        table.text('biography')
        table.string('work_title')
        table.string('work_company')
        table.string('work_email').unique()
        table.string('work_company_url')
        table.string('linkedin_id').unique()
        table.string('linkedin_first_name')
        table.string('linkedin_last_name')
        table.string('linkedin_headline')
        table.string('linkedin_url')
        table.text('linkedin_profile')
        table.string('facebook_id').unique()
        table.string('facebook_name')
        table.string('instagram_id').unique()
        table.string('instagram_username').unique()
        table.integer('instagram_posts').notNullable().defaultTo(0)
        table.integer('instagram_followers').notNullable().defaultTo(0)
        table.decimal('instagram_post_price').notNullable().defaultTo(0)
        table.string('twitter_id').unique()
        table.string('twitter_username').unique()
        table.boolean('enabled').defaultTo(false).notNullable()
        table.timestamps(false, true)
      })
      logger.info(`Created ${tableName} table`)
      const items = [
        {
          full_name: 'KK Chen',
          email: 'kk@bichenkk.com',
          password: '$2a$13$d2loY4ckCVLs42NBviCSyOQBUVuVi.Fs16JU2itDg3tosQD8gGmZy',
          phone_region: 852,
          phone_number: 97189571,
          profile_image_url: 'https://s3-ap-southeast-1.amazonaws.com/binatir.com/assets/customised/avatar-kk.jpg',
          enabled: true,
        },
      ]
      await db.batchInsert(tableName, items).then()
    }
  } catch (error) {
    logger.error(`Error creating ${tableName} table`, error)
  }
}
